package com.sangfor.ssl.service.utils.jni;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import com.sangfor.ssl.service.https.CertType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/* loaded from: classes2.dex */
public class CertUtils {
    private static final String TAG = "CertUtils";

    private static native int nativeGetCertType(String str, String str2);

    private static native String nativeGetCertUsername(String str, String str2);

    private static native boolean nativeVerifyCertPassword(String str, String str2);

    public static boolean verityCertPasword(String str, String str2) throws IllegalArgumentException {
        if (!new File(str).exists()) {
            throw new IllegalArgumentException("certpath not find, certpath = " + str);
        }
        return nativeVerifyCertPassword(str, str2);
    }

    public static CertType getCertType(String str, String str2) throws IllegalArgumentException {
        if (!new File(str).exists()) {
            throw new IllegalArgumentException("certpath not find, certpath = " + str);
        } else if (!nativeVerifyCertPassword(str, str2)) {
            throw new IllegalArgumentException("verity cert password failed.");
        } else {
            return CertType.valueOf(nativeGetCertType(str, str2));
        }
    }

    public static String getCertUsername(String str, String str2) throws IllegalArgumentException {
        if (!new File(str).exists()) {
            throw new IllegalArgumentException("certpath not find, certpath = " + str);
        } else if (!nativeVerifyCertPassword(str, str2)) {
            throw new IllegalArgumentException("verity cert password failed.");
        } else {
            return nativeGetCertUsername(str, str2);
        }
    }

    public static String fromUriGetRealPath(Context context, Uri uri) {
        if (uri == null) {
            return "";
        }
        Uri uri2 = null;
        if ((Build.VERSION.SDK_INT >= 19) && DocumentsContract.isDocumentUri(context, uri)) {
            if (isExternalStorageDocument(uri)) {
                String[] split = DocumentsContract.getDocumentId(uri).split(":");
                if ("primary".equalsIgnoreCase(split[0])) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }
            } else if (isDownloadsDocument(uri)) {
                if (Build.VERSION.SDK_INT >= 24) {
                    return getFilePathForN(context, uri);
                }
                return getDataColumn(context, ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(DocumentsContract.getDocumentId(uri)).longValue()), null, null);
            } else if (isMediaDocument(uri)) {
                String[] split2 = DocumentsContract.getDocumentId(uri).split(":");
                String str = split2[0];
                if ("image".equals(str)) {
                    uri2 = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(str)) {
                    uri2 = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(str)) {
                    uri2 = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
                return getDataColumn(context, uri2, "_id=?", new String[]{split2[1]});
            }
        } else if ("content".equalsIgnoreCase(uri.getScheme())) {
            if (isGooglePhotosUri(uri)) {
                return uri.getLastPathSegment();
            }
            return getDataColumn(context, uri, null, null);
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

    /* JADX WARN: Code restructure failed: missing block: B:13:0x002d, code lost:
        if (r10 != null) goto L6;
     */
    /* JADX WARN: Code restructure failed: missing block: B:14:0x002f, code lost:
        r10.close();
     */
    /* JADX WARN: Code restructure failed: missing block: B:26:0x004e, code lost:
        if (r10 != null) goto L6;
     */
    /* JADX WARN: Code restructure failed: missing block: B:28:0x0051, code lost:
        return null;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:32:0x0056  */
    /* JADX WARN: Type inference failed for: r0v2 */
    /* JADX WARN: Type inference failed for: r0v3, types: [android.database.Cursor] */
    /* JADX WARN: Type inference failed for: r0v4 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private static java.lang.String getDataColumn(android.content.Context r8, android.net.Uri r9, java.lang.String r10, java.lang.String[] r11) {
        /*
            r0 = 1
            java.lang.String[] r3 = new java.lang.String[r0]
            r0 = 0
            java.lang.String r7 = "_data"
            r3[r0] = r7
            r0 = 0
            android.content.ContentResolver r1 = r8.getContentResolver()     // Catch: java.lang.Throwable -> L33 java.lang.Exception -> L35
            r6 = 0
            r2 = r9
            r4 = r10
            r5 = r11
            android.database.Cursor r10 = r1.query(r2, r3, r4, r5, r6)     // Catch: java.lang.Throwable -> L33 java.lang.Exception -> L35
            if (r10 == 0) goto L2d
            boolean r11 = r10.moveToFirst()     // Catch: java.lang.Exception -> L2b java.lang.Throwable -> L52
            if (r11 == 0) goto L2d
            int r11 = r10.getColumnIndexOrThrow(r7)     // Catch: java.lang.Exception -> L2b java.lang.Throwable -> L52
            java.lang.String r8 = r10.getString(r11)     // Catch: java.lang.Exception -> L2b java.lang.Throwable -> L52
            if (r10 == 0) goto L2a
            r10.close()
        L2a:
            return r8
        L2b:
            r11 = move-exception
            goto L37
        L2d:
            if (r10 == 0) goto L51
        L2f:
            r10.close()
            goto L51
        L33:
            r8 = move-exception
            goto L54
        L35:
            r11 = move-exception
            r10 = r0
        L37:
            int r1 = android.os.Build.VERSION.SDK_INT     // Catch: java.lang.Throwable -> L52
            r2 = 24
            if (r1 < r2) goto L47
            java.lang.String r8 = getFilePathForN(r8, r9)     // Catch: java.lang.Throwable -> L52
            if (r10 == 0) goto L46
            r10.close()
        L46:
            return r8
        L47:
            java.lang.String r8 = "CertUtils"
            java.lang.String r9 = "CertUtils getDataColumn fail!"
            com.sangfor.bugreport.logger.Log.error(r8, r9, r11)     // Catch: java.lang.Throwable -> L52
            if (r10 == 0) goto L51
            goto L2f
        L51:
            return r0
        L52:
            r8 = move-exception
            r0 = r10
        L54:
            if (r0 == 0) goto L59
            r0.close()
        L59:
            goto L5b
        L5a:
            throw r8
        L5b:
            goto L5a
        */
        throw new UnsupportedOperationException("Method not decompiled: com.sangfor.ssl.service.utils.jni.CertUtils.getDataColumn(android.content.Context, android.net.Uri, java.lang.String, java.lang.String[]):java.lang.String");
    }

    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    public static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

    private static String getFilePathForN(Context context, Uri uri) {
        try {
            Cursor query = context.getContentResolver().query(uri, null, null, null, null);
            if (query == null) {
                return null;
            }
            int columnIndex = query.getColumnIndex("_display_name");
            query.moveToFirst();
            File file = new File(context.getFilesDir(), query.getString(columnIndex));
            InputStream openInputStream = context.getContentResolver().openInputStream(uri);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            if (openInputStream == null) {
                return null;
            }
            byte[] bArr = new byte[Math.min(openInputStream.available(), 1048576)];
            while (true) {
                int read = openInputStream.read(bArr);
                if (read != -1) {
                    fileOutputStream.write(bArr, 0, read);
                } else {
                    query.close();
                    openInputStream.close();
                    fileOutputStream.close();
                    return file.getPath();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
